//
//  HYButton.m
//  HaiYue
//
//  Created by skyline on 2017/12/19.
//  Copyright © 2017年 skyline. All rights reserved.
//

#import "HYButton.h"
@interface HYButton ()
@end
@implementation HYButton
- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title titleColor:(UIColor *)titleColor
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self setFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self setTitle:title forState:UIControlStateNormal];
        [self setTitleColor:titleColor forState:UIControlStateNormal];
    }
    return self;
}

- (instancetype)initWithTitle:(NSString *)title backColor:(UIColor *)backColor
{
    self = [super init];
    if (self)
    {
        [self setTitle:title forState:UIControlStateNormal];
        [self setBackgroundColor:backColor];
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 5;
        self.touchColor = backColor;
    }
    return self;
}

- (instancetype)initWithTitle:(NSString *)title normalImageName:(NSString *)normalImageName selectImageName:(NSString *)selectImageName
{
    self = [super init];
    if (self)
    {
        [self setTitle:title forState:UIControlStateNormal];
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self setImage:[UIImage imageNamed:normalImageName] forState:UIControlStateNormal];
        [self setImage:[UIImage imageNamed:selectImageName] forState:UIControlStateSelected];
    }
    return self;
}

- (void)setPosition:(HYButtonImagePosition)position
{
    if (_position != position)
    {
        _position = position;
        CGSize imageSize = self.imageView.intrinsicContentSize;
        CGSize titleSize = self.titleLabel.intrinsicContentSize;
        switch (_position)
        {
            case HYButtonImageRightPosition:
                [self setImageEdgeInsets:UIEdgeInsetsMake(0, titleSize.width, 0, - titleSize.width)];
                [self setTitleEdgeInsets:UIEdgeInsetsMake(0, - imageSize.width, 0, imageSize.width)];
                break;
            case HYButtonImageUpPosition:
                [self setImageEdgeInsets:UIEdgeInsetsMake(- titleSize.height / 2 + 0.5, titleSize.width / 2.0 + 0.5, titleSize.height / 2 - 0.5, - titleSize.width / 2.0 - 0.5)];
                [self setTitleEdgeInsets:UIEdgeInsetsMake(imageSize.height / 2, - imageSize.width / 2.0 - 0.5, - imageSize.height / 2, imageSize.width / 2.0 + 0.5)];
                break;
            case HYButtonImageDownPosition:
                [self setImageEdgeInsets:UIEdgeInsetsMake(titleSize.height / 2, titleSize.width / 2, - titleSize.height / 2, titleSize.width / 2)];
                [self setTitleEdgeInsets:UIEdgeInsetsMake(- imageSize.height / 2, -imageSize.width / 2, imageSize.height / 2, imageSize.width / 2)];
                break;
            default:
                break;
        }
    }
}

- (void)setType:(CCShapeLayerType)type
{
    if (_type != type)
    {
        _type = type;
        if (_type == CCShapeLayerTypeCycle || _type == CCShapeLayerTypeEllipse)
        {
            self.layer.masksToBounds = YES;
            self.layer.borderWidth = 0.8;
            self.layer.borderColor = self.titleLabel.textColor.CGColor;
            self.layer.cornerRadius = (_type == CCShapeLayerTypeCycle)?
            (MIN(self.bounds.size.width, self.bounds.size.height)) / 2.0:self.bounds.size.height / 2.0;
        }
        else
        {
            CCShapeLayer *shaper = [[CCShapeLayer alloc]initWithFrame:self.bounds];
            shaper.type = _type;
            shaper.fillColor = nil;
            shaper.strokeColor = self.titleLabel.textColor.CGColor;
            [self.layer addSublayer:shaper];
        }
    }
}
- (void)setAttr:(NSMutableAttributedString *)attr
{
    [self setAttributedTitle:attr forState:UIControlStateNormal];
    CGSize size = [attr.mutableString sizeWithFont:self.font];
    self.bounds = CGRectMake(0, 0, ceil(size.width), ceil(size.height));
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(self.bounds.size);
    }];
}
- (void)setFillColor:(UIColor *)fillColor
{
    if (_fillColor != fillColor)
    {
        _fillColor = fillColor;
        [self setupShaperLayerColor];
    }
}

- (void)setStrokeColor:(UIColor *)strokeColor
{
    if (_strokeColor != strokeColor)
    {
        _strokeColor = strokeColor;
        [self setupShaperLayerColor];
    }
}

//- (void)
- (void)setupShaperLayerColor
{
    if (_type == CCShapeLayerTypeCycle || _type == CCShapeLayerTypeEllipse)
    {
        self.layer.borderColor = (_strokeColor)?_strokeColor.CGColor:self.titleLabel.textColor.CGColor;
        self.backgroundColor = (_fillColor)?_fillColor:self.backgroundColor;
    }
    else{
        for (id layer in [self.layer sublayers])
        {
            if ([layer isKindOfClass:[CCShapeLayer class]])
            {
                CCShapeLayer *shapeLayer = (CCShapeLayer *)layer;
                shapeLayer.fillColor = (_fillColor)?_fillColor.CGColor:shapeLayer.fillColor;
                shapeLayer.strokeColor = (_strokeColor)?_strokeColor.CGColor:shapeLayer.strokeColor;
            }
        }
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.titleLabel.bounds = CGRectMake(0, 0, self.titleLabel.intrinsicContentSize.width, self.titleLabel.intrinsicContentSize.height);
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    self.backgroundColor = self.touchColor;
    self.alpha = 0.3;
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    self.backgroundColor = self.touchColor;
    self.alpha = 1.0;
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
